package org.sky.sys.controller;

import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysOrgan;
import org.sky.sys.service.SysOrganService;
import org.sky.sys.util.RequestUtils;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.sky.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author weifx
 * @Title: SysOrganController
 * @ProjectName springboot-platform
 * @Description: TODO{组织机构管理}
 * @date 2019/1/23 18:18
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/sys/SysOrgan")
public class SysOrganController {
    private Logger logger = LogUtils.getLogger(SysOrganController.class);

    @Autowired
    private SysOrganService sysOrganService;

    /**
     * 打开组织机构管理页面
     * @return
     */
    @GetMapping(value = "/initSysOrganMngPage")
    public String initSysOrganMngPage(){
        return "sys/organ/sysorganmng";
    }

    /**
     * 组织机构树查询
     * @param request
     * @return
     */
    @PostMapping(value = "/selectSysOrganTree/{code}",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String getSysOrganTree(
            @PathVariable String code,
            HttpServletRequest request){
        List list = sysOrganService.getSysOrganTree(code);
        return JsonUtils.obj2json(list);
    }

    /**
     * 打开组织机构添加页面
     * @return
     */
    @GetMapping(value = "/initSysOrganAddPage")
    public String initSysOrganAddPage(){
        return "sys/organ/sysorganedit";
    }

    /**
     * 打开组织机构修改页面
     * @return
     */
    @GetMapping(value = "/initSysOrganEditPage")
    public String initSysOrganEditPage(){
        return "sys/organ/sysorganedit";
    }
    /**
     *删除组织机构
     **/
    @PostMapping(value = "/delSysOrgan",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysOrgan(
            HttpServletRequest request){
        Result result = null;
        try {
            String data=request.getParameter("data");
            SysOrgan so=JsonUtils.json2pojo(data, SysOrgan.class);
            sysOrganService.delSysOrganByCode(so.getCode());
            result = Result.success("删除成功",null,null);
        } catch (ServiceException e) {
            result = Result.error("删除失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }
    /**
     *根据主键查询组织机构表
     **/
    @GetMapping(value = "/getSysOrganById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysOrganById(
            @PathVariable Long id,
            HttpServletRequest request){
        SysOrgan bean = sysOrganService.getSysOrganById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
     *保存{新增/修改}组织机构表
     **/
    @PostMapping(value = "/saveAddEditSysOrgan",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysOrgan(
            HttpServletRequest request){
        Result result = null;
        try {
            SysOrgan edit = RequestUtils.getEntityBean(request,SysOrgan.class);
            sysOrganService.saveAddEditSysOrgan(edit);
            result = Result.success("保存成功",null,null);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("保存失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }
}
